//
//  XKLNavigationController.m
//  GuiZiShouDuanZi
//
//  Created by xmg on 16/4/25.
//  Copyright © 2016年 xiao_kl. All rights reserved.
//

#import "XKLNavigationController.h"

@interface XKLNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation XKLNavigationController

+(void)initialize{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setGes];
   
    [self setNaviBar];
    
}
//<_UINavigationInteractiveTransition: 0x7fd5996b65f0>
//设置手势
-(void)setGes{
    
    //添加全局手势返回
    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
    [self.view addGestureRecognizer:pan];
    
    //把手势代理改为自己,让自己判断是否应该启动手势
    self.interactivePopGestureRecognizer.delegate=self;
    
}


//设置导航条样式
-(void)setNaviBar{
    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
    
    //调整字体样式
    NSMutableDictionary *att=[NSMutableDictionary dictionary];
    att[NSFontAttributeName]=[UIFont systemFontOfSize:20];
    [self.navigationBar setTitleTextAttributes:att];
}


//重写push方法
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    if (self.childViewControllers.count>0) {
        UIButton *but=[UIButton buttonWithType:UIButtonTypeCustom];
        [but setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
        [but setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
        [but setTitle:@"返回" forState:UIControlStateNormal];
        [but setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [but addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
        [but sizeToFit];
        
        UIView *view=[[UIView alloc] initWithFrame:but.frame];
        [view addSubview:but];
        
        viewController.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:view];
        but.contentEdgeInsets=UIEdgeInsetsMake(0, -20, 0, 0);
        //跳转隐藏tabbar条
        viewController.hidesBottomBarWhenPushed=YES;
    }
    
    [super pushViewController:viewController animated:animated];
}

-(void)backClick:(UIButton *)but{
    [self popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    
    //判断是否是根控制器,是就不允许
    if (self.childViewControllers.count==1) {
        return NO;
    }
    
    return YES;
}

@end
